package com.example.annotation;


import java.lang.reflect.Method;

/**
 * @author wxz
 */
public class TestAnnotation {
    public static void main(String[] args) {
        try {
            //获取Student的Class对象
            /**
             * 可以有3中方式获取
             *  1.Class stuClass = Class.forName("com.example.annotation.Student");
             *  2.Student student=new Student();
             *  3. Class stuClass =student.getClass();
             */
            Class stuClass = Student.class;//3
            //说明一下，这里形参不能写成Integer.class，应写为int.class
            Method stuMethod = stuClass.getMethod("study", int.class);
            if (stuMethod.isAnnotationPresent(Annotation.class)) {
                System.out.println("Student类上配置了Annotation注解！");
                //获取该元素上指定类型的注解
                Annotation Annotation = stuMethod.getAnnotation(Annotation.class);
                System.out.println("name: " + Annotation.name() + ", age: " + Annotation.age()
                        + ", score: " + Annotation.score()[0]);
            } else {
                System.out.println("Student类上没有配置Annotation注解！");
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}


